package com.lovo.service.serviceImpl;

import com.lovo.bean.UserRole;
import com.lovo.dao.UserRoleMapper;
import com.lovo.service.UserRoleService;
import com.lovo.util.SqlSessionUtil;
import org.apache.ibatis.session.SqlSession;

public class UserRoleServiceImpl implements UserRoleService {
    @Override
    public boolean deleteByUser(int parseInt) {
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        UserRoleMapper mapper = sqlSession.getMapper(UserRoleMapper.class);
        int n = mapper.deleteByUser(parseInt);
        SqlSessionUtil.commit();
        return n>0;
    }

    @Override
    public boolean addRoleByUser(String value, int parseInt) {
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        UserRoleMapper mapper = sqlSession.getMapper(UserRoleMapper.class);
        int n =0;
        String[] values = value.split(",");
        for (int i = 0; i <values.length; i++) {
            UserRole ur = new UserRole();
            ur.setFkRoleId(Integer.parseInt(values[i]));
            ur.setFkUserId(parseInt);
            int k = mapper.insertSelective(ur);
            if(k>0){
                n=n+1;
            }
        }
        SqlSessionUtil.commit();
        return n>0;
    }

    @Override
    public UserRole selectByUserId(int userId) {
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        UserRoleMapper mapper = sqlSession.getMapper(UserRoleMapper.class);
        UserRole userRole = mapper.selectByUserId(userId);
        SqlSessionUtil.commit();
        return userRole;
    }
}
